import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
import { SubscriptionsService } from './subscriptions.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../../common/guards/roles.guard';
import { Roles, Role } from '../../common/decorators/roles.decorator';
import { SubscriptionPlan } from './entities/subscription-plan.entity';

/**
 * 订阅控制器
 */
@Controller('subscriptions')
export class SubscriptionsController {
  constructor(private readonly subscriptionsService: SubscriptionsService) {}

  /**
   * 获取所有启用的订阅套餐（用户端）
   */
  @Get('plans')
  getPlans() {
    return this.subscriptionsService.getPlans();
  }

  /**
   * 获取所有套餐（包括未启用，管理员）
   */
  @Get('admin/plans')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.SUPER_ADMIN, Role.ADMIN)
  getAllPlans() {
    return this.subscriptionsService.getAllPlans();
  }

  /**
   * 创建套餐（管理员）
   */
  @Post('admin/plans')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.SUPER_ADMIN, Role.ADMIN)
  createPlan(@Body() data: Partial<SubscriptionPlan>) {
    console.log('🆕 创建新套餐:', data);
    return this.subscriptionsService.createPlan(data);
  }

  /**
   * 更新套餐（管理员）
   */
  @Put('admin/plans/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.SUPER_ADMIN, Role.ADMIN)
  updatePlan(@Param('id') id: string, @Body() data: Partial<SubscriptionPlan>) {
    console.log(`📝 更新套餐 #${id}:`, data);
    return this.subscriptionsService.updatePlan(parseInt(id), data);
  }

  /**
   * 删除套餐（管理员）
   */
  @Delete('admin/plans/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles(Role.SUPER_ADMIN, Role.ADMIN)
  async deletePlan(@Param('id') id: string) {
    console.log(`🗑️ 删除套餐 #${id}`);
    await this.subscriptionsService.deletePlan(parseInt(id));
    return { message: '删除成功' };
  }
}





